package com.homework.phase1.module4;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class InfoManagement {

    //集合用于存放学生信息
    List<Object> list = new LinkedList<>();

    //创建缓冲字符流
    BufferedReader br = null;

    {
        try {
            br = new BufferedReader(new FileReader("G:/hadoop/student.txt"));
            //从文件中提取出来的数据，存放该行字符串
            String s = null;
            //将该行字符串进行解析，分割成字符串数组
            String[] str = null;
            while ((s = br.readLine()) != null) {
                //通过之前的toString方法的重建，可以用空格进行分割字符串，再放入字符串数组中
                str = s.split(" ");
                //将字符串数据的数据按下标进行提取，创建student类对象
                list.add(new Student(str[0],str[1],Integer.parseInt(str[2])));
            }
        }  catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != br) {

                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

        }
    }




    Scanner sc = new Scanner(System.in);
    //添加学生
    public void addStudent() {

        System.out.println("请输入学号、姓名、年龄");

        String id = sc.next();
        String name = sc.next();
        int age = sc.nextInt();

        boolean b = false;
        //如果对象的id或age发出异常，或name为null,则不往list数组中存放数据
        Student s = new Student(id, name, age);
        if (s.getId() !=null && s.getName() !=null && s.getAge() != 0){
            b = list.add(s);
        }


        if (b == true) {

            System.out.println("添加成功");
        } else {

            System.out.println("添加失败");
        }
    }

    //删除学生信息
    public void removeStudent() {
        System.out.println("请输入学号、姓名、年龄");
        String id = sc.next();
        String name = sc.next();
        int age = sc.nextInt();
        boolean b = list.remove(new Student(id, name ,age));

        if (b == true) {

            System.out.println("删除成功");
        } else {

            System.out.println("删除失败");
        }
    }
    //修改学生信息
    public void modifyStudent() {

        System.out.println("请输入学号、姓名、年龄");
        String id = sc.next();
        String name = sc.next();
        int age = sc.nextInt();


        if (list.contains(new Student(id, name, age))) {
            System.out.println("请要修改成的学号、姓名、年龄");
            String id1 = sc.next();
            String name1 = sc.next();
            int age1 = sc.nextInt();
            Student s = (Student) list.set(list.indexOf(new Student(id, name, age)),new Student(id1, name1 ,age1));
            System.out.println("修改成功");

        } else {
            System.out.println("查无此人");
        }

    }

    //查找学生信息
    public void findStudent() {
        System.out.println("请输入学号、姓名、年龄");
        String id = sc.next();
        String name = sc.next();
        int age = sc.nextInt();

        Student s = new Student(id, name, age);
        if (list.contains(s)) {

            System.out.println("已查找到此人");
            System.out.println("该学生信息为：" + s);


        } else {
            System.out.println("查无此人");
        }
    }
    //遍历学生信息
    public List<Object> allStudent(){

        return list;

    }
}
